package com.example.server.command;

import java.util.Map;

import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

public class CommandFactory {

	private final static Logger log = Log.getLogger(CommandFactory.class);

	private Map<String, Object> config;

	private static CommandFactory commandFactory = null;

	public CommandFactory() {
		commandFactory = this;
	}

	public static CommandFactory getInstance() {
		if (commandFactory == null)
			return new CommandFactory();
		else
			return commandFactory;
	}

	public AbstractCommand createCommand(String method) {

		AbstractCommand abstractCommand = null;

		if (config.containsKey(method)) {
			String className = config.get(method).toString();
			try {
				@SuppressWarnings("unchecked")
				Class<AbstractCommand> clazz = (Class<AbstractCommand>) Class.forName(className);

				abstractCommand = clazz.newInstance();
				
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		} else {
			log.warn("没有找到该方法：" + method);
		}
		return abstractCommand;
	}

	public Map<String, Object> getConfig() {
		return config;
	}

	public void setConfig(Map<String, Object> config) {
		this.config = config;
	}

}
